package com.heluo.ucenterservice.service.impl;

import com.heluo.commonutils.JwtUtils;
import com.heluo.commonutils.MD5;
import com.heluo.servicebase.exceptionhandler.HeluoException;
import com.heluo.ucenterservice.entity.UcenterMember;
import com.heluo.ucenterservice.entity.vo.RegisterVo;
import com.heluo.ucenterservice.mapper.UcenterMemberMapper;
import com.heluo.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {

        String mobile=member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new HeluoException(20001,"登陆失败");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null){
            throw new HeluoException(20001,"手机号不存在");
        }

        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new HeluoException(20001,"密码错误");
        }
        if (mobileMember.getIsDisabled()){
            throw new HeluoException(20001,"您已被禁用");
        }

        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        ;
        

        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new HeluoException(20001,"登陆失败");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)){
            throw new HeluoException(20001,"注册失败");
        }
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new HeluoException(20001,"注册失败");
        }
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://cn.bing.com/images/search?view=detailV2&ccid=wm9D2YsG&id=A72E8D78FCFABD43C3CC97A3BEADB41693336017&thid=OIP.wm9D2YsGrIOhIo4wtEMoSAHaGP&mediaurl=https%3a%2f%2ftse1-mm.cn.bing.net%2fth%2fid%2fR-C.c26f43d98b06ac83a1228e30b4432848%3frik%3dF2Azkxa0rb6jlw%26riu%3dhttp%253a%252f%252fimg.ewebweb.com%252fuploads%252f20200219%252f14%252f1582093450-hHUCNepnVA.png%26ehk%3d3Zx0%252b8d79SRJoPC0XAlegAhImT%252fdiiOaD32qHY9RLis%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&exph=586&expw=695&q=%e8%b7%af%e9%a3%9e%e5%9b%be%e7%89%87&simid=608028641966053047&FORM=IRPRST&ck=486DEA765326178A00B3856481718070&selectedIndex=2");

        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegsiterDay(String day) {
        return baseMapper.countRegsiterDay(day);
    }
}
